# 创建时间:2023/5/20 23:03
# 创建人:天霄
# 计划 :SenjoTwintail
# 文件 :Effect.py
# 基于 Python 3.8.2
# ========================================
# 特效、动效
# ========================================
import math
import textwrap
import asyncio
import pygame
from pygame import freetype

from Api import savings, uiApi
from BasePygame.Gamer import first, change_image, LoadImage, imageLoad_from_plist
from BasePygame.Public import flagBox, textOutline
from Common.Common import text2paragraph


def game_now():
    """获取游戏时刻"""
    return pygame.time.get_ticks()


class Effect:
    def __init__(self, defaultFont: freetype.Font):
        """特效与动效"""
        self.font = defaultFont
        self.color_pink = (247, 239, 250)
        self.color_pinkDark = (144, 81, 123)
        self._t_favorLevelExpGet = self._t_favorLevelUp = 0
        self._t_favorLevelExpGet_Changed = self._t_favorLevelUp_Changed = False
        asyncio.run(self.main())

    async def main(self):
        """初始化"""
        await asyncio.gather(self._get_favor_base1(), )

    async def _get_favor_base1(self):
        """获取宿舍相关特效图像"""
        self.favorscoreupeffect = imageLoad_from_plist(*uiApi.getEffectPathPlist('favorscoreupeffect'))
        self.favorupeffect = imageLoad_from_plist(*uiApi.getEffectPathPlist('favorupeffect'))

    @property
    def t_favorLevelExpGet(self):
        """好感度经验获得的时刻"""
        return self._t_favorLevelExpGet

    @t_favorLevelExpGet.setter
    def t_favorLevelExpGet(self, value):
        """记录好感度经验获得的时刻"""
        self._t_favorLevelExpGet = value
        self._t_favorLevelExpGet_Changed = True

    def favorLevelExpGet(self, exp: int, font=None):
        """增加好感值经验（显示1.5秒）"""
        if not self._t_favorLevelExpGet_Changed: return
        t = game_now() - self.t_favorLevelExpGet
        if t > 1500:
            self._t_favorLevelExpGet_Changed = False
            return
        if font is None:
            font = self.font
        screen = first.screen
        favorscoreupeffect = self.favorscoreupeffect
        bg4 = favorscoreupeffect['bg4']  # 半透明蓝色背景
        coldlove = favorscoreupeffect['coldlove']  # 心碎
        love = favorscoreupeffect['love']  # 爱心
        # birthhat = favorscoreupeffect['birthhat']  # 生日帽
        # birthlove2 = favorscoreupeffect['birthlove2']  # 生日爱心
        # day = favorscoreupeffect['day']  # 生日
        # xin = favorscoreupeffect['xin']  # 边缘有白线的爱心

        biglove = love.copy().drag(0.9, 'wh')
        leftlove = love.copy().drag(0.3, 'wh')
        ringhtlove = love.copy().drag(0.2, 'wh')
        bigloveframe = (0, 0, biglove.clickRect.w, bg4.clickRect.h)
        leftloveframe = (0, 0, bg4.clickRect.h/2, bg4.clickRect.h/2)
        ringhtloveframe = (biglove.clickRect.w-bg4.clickRect.h/2, bg4.clickRect.h/2, bg4.clickRect.h/2, bg4.clickRect.h/2)

        # expmsg = font.render(f'+{exp}', self.color_pink, style=freetype.STYLE_STRONG, size=60)[0]
        expmsg = textOutline(font, f'+{exp}', self.color_pinkDark, 3, fgcolor=self.color_pink, size=55)
        expmsgframe = (biglove.clickRect.w, 0, bg4.clickRect.w-biglove.clickRect.w, bg4.clickRect.h)
        bar = bg4.copy().draw_RectCustom(biglove, bigloveframe, 'center').draw_RectCustom(leftlove, leftloveframe, 'center').\
            draw_RectCustom(ringhtlove, ringhtloveframe, 'center').draw_RectCustom(expmsg, expmsgframe, 'center')
        x = first.screen_center[0]
        y = first.screen_center[1]
        if t > 200:
            y = y - 0.05*t
            bar.surface.set_alpha(255-0.255*t)
        bar.rect.center = x, y
        screen.blit(bar.surface, bar.rect)

    @property
    def t_favorLevelUp(self):
        """好感度升级的时刻"""
        return self._t_favorLevelUp

    @t_favorLevelUp.setter
    def t_favorLevelUp(self, value):
        """记录好感度升级的时刻"""
        self._t_favorLevelUp = value
        self._t_favorLevelUp_Changed = True

    def favorLevelUp(self):
        """好感度升级（显示2秒）"""
        if not self._t_favorLevelUp_Changed: return
        t = game_now() - self.t_favorLevelUp
        if t > 2000:
            self._t_favorLevelUp_Changed = False
            return
        favorupeffect = self.favorupeffect
        bg4 = favorupeffect['bg4']  # 白色背景
        heart = favorupeffect['heart']  # 爱心
        up = favorupeffect['up']  # “好感UP！！”



